var ManageList = {};
ManageList.reloadGrid = function(url){
    $("#gridTable").jqGrid('setGridParam',{url:url,page:1}).trigger("reloadGrid");
};

/*channels*/
ManageList.channels={};
ManageList.channels.getChannelTree = function(){
    $.get("getChannelTree.jspx?format=json",function(data){
        var settings = {
            data:{
                simpleData:{
                    enable:true
                }
            },
            callback:{
                onClick:onClick
            }
        };
        var nodes = data.ztree;
        $.fn.zTree.init($("#channel-tree"),settings,nodes);
    });

    function onClick(event, treeId, treeNode, clickFlag){
        var id = treeNode.id;
        var isParent = treeNode.isParent;
        var url = "getChannels.jspx?id="+id;
        if(isParent){
            ManageList.channels.setCaption(treeNode.name);
            ManageList.reloadGrid(url);
        }
        else{
            location.href="preEdit.jspx?id="+id;
        }
    };
};

ManageList.channels.getSortChannelTree = function(){
    $.get("getChannelTree.jspx?format=json",function(data){
        var settings = {
            data:{
                simpleData:{
                    enable:true
                }
            },
            callback:{
                onClick:onClick
            }
        };
        var nodes = data.ztree;
        $.fn.zTree.init($("#channel-tree"),settings,nodes);
    });

    function onClick(event, treeId, treeNode, clickFlag){
        var id = treeNode.id;
        var isParent = treeNode.isParent;
        
        ManageList.reloadSortGrid(isParent,id);
        
    };
};

ManageList.reloadSortGrid = function(isParent,id){
	var url = "getChannels.jspx?format=json&pageable=false&id="+id;
	if(isParent){
		url = "getChannels.jspx?format=json&pageable=false&id="+id;
	}else{
		url = "getChannels.jspx?format=json&action=get&id="+id;
	}
	
	$("#isParent").val(isParent);
	$("#current_id").val(id);
	
	$.get(url,function(data){
		var datarows = data.datarows;
		var gridTable = $("#gridTable");
		gridTable.find("tbody").empty();
		if(datarows!=undefined){
			$.each(datarows,function(index,element){
				var tr = $("<tr>");
				tr.append("<td>"+element.name+"</td>");
				tr.append("<td><input type='text' name='orderid_"+element.id+"' value='"+element.orderid+"'></td>");
				gridTable.append(tr);
			});
		}
	});
};
ManageList.channels.getChannels = function(parentId,url,rowNum){
    var grid_selector = "#gridTable";
    var pager_selector = "#gridPager";
    var colNames = ["","序号","名称","点击数","排序号","添加时间","修改时间"];
    var colModel = [
        {name:"myac",fixed:true,width:55,formatter:customActions,formatoptions:{
            keys:true,
            editformbutton:false,
            editUrl:"preEdit.jspx?id=",
            delUrl:"delete.jspx?id="}
        },
        {name:"id",index:"id",width:10,key:true},
        {name:"name",index:"name",width:60},
        {name:"hits",index:"hits",width:25},
        {name:"orderid",index:"orderid",width:20},
        {name:"addtime",index:"addtime",width:35},
        {name:"edittime",index:"edittime",width:35}
    ];
    myCustomGrid(grid_selector,pager_selector,url,colNames,colModel,"栏目管理","auto","preAdd.jspx","preEdit.jspx?id=","delete.jspx");
};
ManageList.channels.setCaption = function(name){
    name = "["+name+"] 栏目管理";
    $("#gridTable").jqGrid('setCaption',name);
};

ManageList.channels.getParentChannelsTree = function(){
    var parentChannelObject = $("#parentChannel");
    var offset = parentChannelObject.offset();
    $("#channelContent").css({left:offset.left+"px", top:offset.top + parentChannelObject.outerHeight()+"px" }).slideDown("fast");

    $.get("getChannelTree.jspx?type=1&format=json",function(data){
        var settings = {
            data:{
                simpleData:{
                    enable:true
                }
            },
            callback:{
                onClick:onClick
            }
        };
        var nodes = data.ztree;
        $.fn.zTree.init($("#treeChannels"),settings,nodes);
    });
    function onClick(event, treeId, treeNode, clickFlag){
        var id = treeNode.id;
        $("#parentid").val(id);
        $("#parentChannel").val(treeNode.name);
        $("#channelContent").fadeOut("fast");
    }
};

/*用于在preEdit和preAdd中修改Channel的内部链接和外部链接时使用*/
ManageList.channels.changeChannelType = function(channelType){
    if(channelType==0){
        $("#div_linkurl").css("display","none");
        $("#div_shortdesc").css("display","block");
        $("#div_description").css("display","block");

        $("#upload2").css("display","none");
        CustomUploader.uploadFiles.destory("linkurl","upload2");
    }else if(channelType==1){
        $("#div_linkurl").css("display","none");
        $("#div_shortdesc").css("display","none");
        $("#div_description").css("display","none");

        $("#upload2").css("display","none");
        CustomUploader.uploadFiles.destory("linkurl","upload2");
    }else if(channelType==2){
        $("#div_linkurl").css("display","none");
        $("#div_shortdesc").css("display","none");
        $("#div_description").css("display","none");

        $("#upload2").css("display","none");
        CustomUploader.uploadFiles.destory("linkurl","upload2");
    }else if(channelType==3){
        $("#div_linkurl").css("display","none");
        $("#div_shortdesc").css("display","block");
        $("#div_description").css("display","block");

        $("#upload2").css("display","none");
        CustomUploader.uploadFiles.destory("linkurl","upload2");
    }else{
        $("#div_linkurl").css("display","block");
        $("#div_shortdesc").css("display","none");
        $("#div_description").css("display","none");

        $("#upload2").css("display","block");
        CustomUploader.uploadFiles.init("linkurl","upload2");
        $(".webuploader-container").css("position",null);
    }
};


/* articles */
ManageList.articles = {};
ManageList.articles.getChannelList = function(select_id,channelId,ctx){
    var typeid_url = ctx+"/manage/channel/getJson.jspx?format=json&cattype=2&pageable=false";
    $.get(typeid_url,function(data){
        var select_object = $("#"+select_id);
        select_object.empty();
        select_object.append('<option value="0" selected="selected">请选择类别</option>');
        $.each(data.datarows,function(index,element){
            select_object.append('<option value="'+element.id+'">'+element.name+'</option>');
        });
        if(channelId!==undefined && channelId!=-1) {
            $("#" + select_id).val(channelId);
        }
    });
};
ManageList.articles.openLinkurl = function(show){
    if(show==1){
        $("#linkurl-div").css("display","block");
        $("#upload2").css("display","block");
        CustomUploader.uploadFiles.init("linkurl","upload2");
    }else{
        $("#linkurl-div").css("display","none");
        $("#upload2").css("display","none");
        CustomUploader.uploadFiles.destory("upload2");
    }
};
ManageList.articles.changeChannel = function(){
	var typeid_select = $("#channelId");
	typeid_select.change(function(){
		var channel_id = typeid_select.find("option:selected").val();
		ManageList.reloadGrid("getJson.jspx?format=json&channelid="+channel_id);
	});
};
ManageList.articles.getJson = function(url){
	var grid_selector = "#gridTable";
	var pager_selector = "#gridPager";
	var colNames = ["","序号","栏目","标题","点击数","添加时间"];
	var colModel = [
			{name:"myac",index:"myac",fixed:true,width:80,formatter:customActions,formatoptions:{
			keys:false,
			editformbutton:false,
			editUrl:"preEdit.jspx?id=",
			delUrl:"delete.jspx",
			seachable:false
			}},
			{name:"id",index:"id",width:15,key:true},
			{name:"channel.name",index:"channel.name",width:40},
			{name:"title",index:"title",width:80},
			{name:"hits",index:"hits",width:25},
			{name:"addtime",index:"addtime",width:35}
		];
	myCustomGrid(grid_selector,pager_selector,url,colNames,colModel,"",'auto',"preAdd.jspx","preEdit.jspx?id=","delete.jspx?id=");
};

/** news */
ManageList.news={};
ManageList.news.getNewsChannels = function(select_id,newsChannelId,ctx){
    var typeid_url = ctx+"/manage/news_channel/getJson.jspx?format=json&pageable=false";
    $.get(typeid_url,function(data){
        var select_object = $("#"+select_id);
        select_object.empty();
        select_object.append('<option value="0">请选择类别</option>');
        $.each(data.datarows,function(index,element){
            select_object.append('<option value="'+element.id+'">'+element.name+'</option>');
        });
        if(newsChannelId!=-1) {
            select_object.val(newsChannelId);
        }
    });
};

ManageList.news.openLinkurl = function(show){
    if (show != 1) {
        $("#linkurl").css("display", "none");
        CustomUploader.uploadFiles.destory("upload2");
    } else {
        $("#linkurl").css("display", "block");
        CustomUploader.uploadFiles.init("linkurl", "upload2");
    }
};

ManageList.news.changeType = function(){
	var typeid_select = $("#newsChannelId");
	typeid_select.change(function(){
		var newsChannelId = typeid_select.find("option:selected").val();
		ManageList.reloadGrid("getJson.jspx?format=json&newsChannelId="+newsChannelId);
	});
};

ManageList.news.getJson = function(url){
	var grid_selector = "#gridTable";
	var pager_selector = "#gridPager";
	var colNames = ["","序号","类别","标题","作者","编辑","来源","点击数","添加时间"];
	var colModel = [
			{name:"myac",index:"myac",fixed:true,width:60,formatter:customActions,formatoptions:{
			keys:false,
			editformbutton:false,
			editUrl:"preEdit.jspx?id=",
			delUrl:"delete.jspx"
			}},
			{name:"id",index:"id",width:10,key:true},
			{name:"newsChannel.name",index:"newsChannel.name",width:20},
			{name:"title",index:"title",width:70},
			{name:"author",index:"author",width:15},
			{name:"inputer",index:"inputer",width:10},
			{name:"copyfrom",index:"copyfrom",width:15},
			{name:"hits",index:"hits",width:10},
			{name:"addtime",index:"addtime",width:25}
		];
	myCustomGrid(grid_selector,pager_selector,url,colNames,colModel,"",'auto',"preAdd.jspx","preEdit.jspx?id=","delete.jspx?id=");
};